package com.blogspot.nhu313.myhistory.service.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.blogspot.nhu313.myhistory.category.CategorySQLStringBuilder;

public class DBServiceImpl extends SQLiteOpenHelper implements DBService {
    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "myhistory.db";
    
    SQLiteDatabase db;
    
    public DBServiceImpl(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CategorySQLStringBuilder.createTable());
    }
    
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(CategorySQLStringBuilder.deleteTable());
        onCreate(db);
    }
    
    public void open(){
    	db = this.getWritableDatabase();
    }
    
    public void close(){
    	super.close();
    }
    
    public long add(String tableName, ContentValues values){
    	long id = db.insert(tableName, null, values);
    	return id;
    }
    
    /**
     * @param SQL query string
     * @return Cursor pointer to the an element before first
     * TODO fix comment
     */
    public Cursor get(String query){
    	Cursor cursor = db.rawQuery(query, null);
    	return cursor;
    }
	
}